package S17.second;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;

public class S03 {
	public static void Hello(String who) {
		System.out.println("Hello "+who);
	}
	public static String run(Code code) { //返回code运行时标准输出的内容
		OutputStream os= new ByteArrayOutputStream();
		PrintStream ps = new PrintStream(os, true);
		PrintStream out = System.out; //保存原来的标准输出
		System.setOut(ps); //设置新的标准输出
		code.runCode(); //要运行的代码
		System.setOut(out); //还原原来的标准输出
		return os.toString(); //返回代码运行时通过标准输出 输出的内容
	}
	public static void main(String[] args) {
		String r=run(()->{
			System.out.println("How are you?");
			System.out.println("I'm fine, thank you.");
			System.out.println("And you?");
			System.out.println("Fine, thanks.");
		});
		System.out.println(r);
		System.out.println("---------------------");
		r=run(()->{
			Hello("张三");
		});
		System.out.println(r);
		System.out.println("=====================");
		r=run(()->{
			S01.main(null);
		});
		System.out.println(r);
	}

}
